package com.jwsoft.manager.core.integration.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.*;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.engine.cache.jetcache.utils.JetCacheUtils;
import com.bifang.module.base.core.helper.DictHelper;
import com.bifang.module.base.core.integration.DictIntegration;
import com.bifang.module.system.common.enums.ArticleStatusEnum;
import com.bifang.module.system.common.vo.appArticle.AppArticleVO;
import com.bifang.module.system.core.dao.service.SysOrgService;
import com.bifang.module.system.core.integration.AppArticleIntegration;
import com.jwsoft.manager.common.constant.EduCache;
import com.jwsoft.manager.common.enums.ConfigNameAliasEnum;
import com.jwsoft.manager.common.enums.SchoolNatureEnum;
import com.jwsoft.manager.common.vo.eduEnrollmenBrochure.EnrollmenVO;
import com.jwsoft.manager.common.vo.eduFormEnroll.EduFormEnrollKeyVO;
import com.jwsoft.manager.common.vo.eduFormEnroll.EduFormEnrollSchoolVO;
import com.jwsoft.manager.common.vo.eduFormEnroll.EduFormEnrollVO;
import com.jwsoft.manager.common.vo.eduFormEnroll.SelectableConfigName;
import com.jwsoft.manager.common.vo.eduFormSpecial.EduFormSpecialVO;
import com.jwsoft.manager.common.vo.eduSchool.EduSchoolVO;
import com.jwsoft.manager.core.dao.model.*;
import com.jwsoft.manager.core.dao.service.*;
import com.jwsoft.manager.core.integration.EduEnrollmenBrochureJhIntegration;
import com.jwsoft.manager.core.util.EduHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 各学校招生简章业务实现类
 *
 * @author chenchao
 * @since 2023-01-11
 */
@ApiService(funcCode = "EduenrollmenJh", title = "各学校招生简章")
public class EduEnrollmenBrochureJhIntegrationImpl implements EduEnrollmenBrochureJhIntegration {

    @Autowired
    public EduEnrollmenBrochureService eduEnrollmenBrochureService;
    @Autowired
    private EduSchoolService eduSchoolService;
    @Autowired
    private AppArticleIntegration appArticleIntegration;
    @Autowired
    private EduFormEnrollService eduFormEnrollService;
    @Autowired
    private EduHelper eduHelper;
    @Autowired
    public DictIntegration dictIntegration;
    @Autowired
    public EduFormEnrollBaseService eduFormEnrollBaseService;
    @Autowired
    public EduFormEnrollParamsService eduFormEnrollParamsService;
    @Autowired
    public EduFormEnrollParamsBaseService eduFormEnrollParamsBaseService;
    @Autowired
    public SysOrgService sysOrgService;


    @Override
    @OpApi(funcCode = "EduenrollmenJh0011", title = "新增或更新招生方案", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "EduenrollmenJh0011",
            operationName = "新增或更新招生方案",
            dataType = "articleId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public String saveEnroll(EnrollmenVO vo) {

        if (ObjectUtils.isEmpty(vo.getYear())) {
            vo.setYear(eduHelper.thisYear());
        }
        String orgId = SessionUtil.getSessionInfo().getOrgId();
        if (StringUtils.isBlank(vo.getSchoolId())) {
            vo.setSchoolId(orgId);
            vo.setOrgId(orgId);
        } else {
            vo.setOrgId(vo.getSchoolId());
        }
        EduSchoolVO eduSchoolVO = eduSchoolService.getDetail(vo.getSchoolId());
        if (eduSchoolVO == null) {
            throw new AppException("学校信息不存在！");
        }
        Set<String> systemPrivileges = SessionUtil.getSystemPrivileges();
        boolean supAdmin = false;
        if (!CollectionUtils.isEmpty(systemPrivileges) && systemPrivileges.contains(SysPermissionEnum.superadmin.getType())) {
            supAdmin = true;
        }else{
            //判断登录人的账号是否为区管理员
            EduSchoolVO detail = eduSchoolService.getDetail(orgId);
            if (ObjectUtils.isEmpty(detail)){
                supAdmin = true;
            }
        }
        AppArticleVO articleVO = Convert.convert(AppArticleVO.class, vo);

        Long articleId;
        if (ObjectUtils.isEmpty(vo.getArticleId())) {
            if (StringUtils.isBlank(articleVO.getShowPosition())) {
                articleVO.setShowPosition("20");
            }
            if (StringUtils.isBlank(articleVO.getShowType())) {
                articleVO.setShowType("0");
            }
            articleVO.setPublisher(eduSchoolVO.getSchoolName());
            articleVO.setAuthStatus(AuthStatusEnum.NODO.getType());
            articleVO.setAuthTime(null);
            articleVO.setAuthRemarks(null);
            articleId = appArticleIntegration.add(articleVO).getArticleId();
        } else {
            if (supAdmin) {
                articleId = appArticleIntegration.editAdmin(articleVO).getArticleId();
            } else {
                articleId = appArticleIntegration.edit(articleVO).getArticleId();
            }
        }

        if (StringUtils.isBlank(vo.getAreaCode())) {
            vo.setAreaCode(SessionUtil.getSessionInfo().getAreaCode());
        }
        if (StringUtils.isBlank(vo.getAreaCode())) {
            throw new AppException("行政区划不能为空！");
        }

        EduEnrollmenBrochure eduEnrollmenBrochure = Convert.convert(EduEnrollmenBrochure.class, vo);
        //不开通服务区
        if(eduEnrollmenBrochure.getServiceFlag().equalsIgnoreCase(BoolEnum.FALSE.getType())){
            eduEnrollmenBrochure.setHouseholdEndTimeFw(null);
            eduEnrollmenBrochure.setHouseEndTimeFw(null);
        }
        //不限制房产截止时间（限制0，不限1）
        if (eduEnrollmenBrochure.getHouseEndTimeNo().equalsIgnoreCase(BoolEnum.TRUE.getType())){
            eduEnrollmenBrochure.setHouseEndTime(null);
        }
        if (eduEnrollmenBrochure.getHouseholdEndTimeNo().equalsIgnoreCase(BoolEnum.TRUE.getType())){
            eduEnrollmenBrochure.setHouseholdEndTime(null);
        }
        if (!CollectionUtils.isEmpty(vo.getEnrollAreaList())){
            String enrollArea = String.join(",", vo.getEnrollAreaList());
            eduEnrollmenBrochure.setEnrollArea(enrollArea);
        }else{
            eduEnrollmenBrochure.setEnrollArea(null);
        }
        if (!CollectionUtils.isEmpty(vo.getEnrollAreaConfigList())){
            eduEnrollmenBrochure.setAdmissionsCategory(JSON.toJSONString(vo.getEnrollAreaConfigList()));
        }else{
            eduEnrollmenBrochure.setAdmissionsCategory("");
        }
        eduEnrollmenBrochure.setArticleId(articleId);
        eduEnrollmenBrochure.setSchoolId(vo.getOrgId());
        if (ObjectUtils.isEmpty(eduEnrollmenBrochure.getEndRegistration())) {
            eduEnrollmenBrochure.setEndRegistration(BoolEnum.FALSE.getType());
        }
        int lastYear = vo.getYear() - 1;
        QueryWrapper<EduEnrollmenBrochure> oldBrochureQueryWrapper = new QueryWrapper<>();
        oldBrochureQueryWrapper.lambda().eq(EduEnrollmenBrochure::getSchoolId, vo.getOrgId())
                .eq(EduEnrollmenBrochure::getYear, lastYear);
        EduEnrollmenBrochure oldBrochure = eduEnrollmenBrochureService.getOne(oldBrochureQueryWrapper, false);
        if (oldBrochure != null) {
            if (!StringUtils.isBlank(oldBrochure.getMapRegion())) {
                eduEnrollmenBrochure.setMapRegion(oldBrochure.getMapRegion());
            }
        }
        if (ObjectUtils.isEmpty(eduEnrollmenBrochure.getEnrollmenBrochureId())) {
            LambdaQueryWrapper<EduEnrollmenBrochure> queryEnrollmen  = new QueryWrapper<EduEnrollmenBrochure>().lambda().eq(EduEnrollmenBrochure::getSchoolId, eduSchoolVO.getOrgId()).eq(EduEnrollmenBrochure::getYear, vo.getYear());
            List<EduEnrollmenBrochure> brochureList = eduEnrollmenBrochureService.list(queryEnrollmen);
            if (brochureList.size()>1){
                throw new AppException("已存在招生简章不能新增！");
            }
            eduEnrollmenBrochure.setEnrollmenBrochureId(IdUtil.simpleUUID());
            eduEnrollmenBrochureService.save(eduEnrollmenBrochure);
        } else {
            if (!supAdmin) {
                QueryWrapper<EduFormEnroll> formQueryWrapper = new QueryWrapper<>();
                formQueryWrapper
                        .lambda()
                        .eq(EduFormEnroll::getFormRefId, eduEnrollmenBrochure.getEnrollmenBrochureId())
                        .eq(EduFormEnroll::getValidStatus, BoolEnum.TRUE.getType())
                        .eq(EduFormEnroll::getFormRefName, "edu_enrollmen_brochure");
                List<EduFormEnroll> formList = eduFormEnrollService.list(formQueryWrapper);
                if (formList.size() > 0) {
                    formList.forEach(
                            item -> {
                                item.setStatus(ArticleStatusEnum.draft.getType());
                                item.setValidStatus(BoolEnum.TRUE.getType());
                            });
                    eduFormEnrollService.updateBatchById(formList);
                }
            }
            //更新招生方案
            eduEnrollmenBrochureService.updateById(eduEnrollmenBrochure);
        }
        //查询招生表单edu_form_enroll_base保存到edu_form_enroll表中
        String SchoolNature = null;
        LambdaQueryWrapper<EduFormEnrollBase> enrollBaseQueryWrapper = new LambdaQueryWrapper<>();
        enrollBaseQueryWrapper.eq(EduFormEnrollBase::getYear, vo.getYear())
                .eq(EduFormEnrollBase::getAreaCode, vo.getAreaCode());
        if (eduSchoolVO.getSchoolNature().equals(SchoolNatureEnum.gb.getType())) {
            enrollBaseQueryWrapper.eq(EduFormEnrollBase::getSchoolNature, SchoolNatureEnum.gb.getType());
            SchoolNature=SchoolNatureEnum.gb.getName();
        }
        if (eduSchoolVO.getSchoolNature().equals(SchoolNatureEnum.mb.getType())) {
            enrollBaseQueryWrapper.eq(EduFormEnrollBase::getSchoolNature, SchoolNatureEnum.mb.getType());
            SchoolNature=SchoolNatureEnum.mb.getName();
        }
        if (eduSchoolVO.getSchoolNature().equals(SchoolNatureEnum.mzg.getType())) {
            enrollBaseQueryWrapper.eq(EduFormEnrollBase::getSchoolNature, SchoolNatureEnum.mzg.getType());
            SchoolNature=SchoolNatureEnum.mzg.getName();
        }
        List<EduFormEnrollBase> eduFormEnrollBaseList = eduFormEnrollBaseService.list(enrollBaseQueryWrapper);
        if (CollectionUtils.isEmpty(eduFormEnrollBaseList)) {
            throw new AppException("区统一"+SchoolNature+"学校招生表单未配置！");
        }
        //更新school表
        LambdaUpdateWrapper<EduSchool> updateWrapper = new UpdateWrapper<EduSchool>().lambda()
                .set(EduSchool::getHouseEndTimeNo, eduEnrollmenBrochure.getHouseEndTimeNo())
                .set(EduSchool::getHouseEndTime, eduEnrollmenBrochure.getHouseEndTime())
                .set(EduSchool::getHouseholdEndTimeNo, eduEnrollmenBrochure.getHouseholdEndTimeNo())
                .set(EduSchool::getHouseholdEndTime, eduEnrollmenBrochure.getHouseholdEndTime())
                .set(EduSchool::getServiceFlag, eduEnrollmenBrochure.getServiceFlag())
                .set(EduSchool::getHouseEndTimeFw, eduEnrollmenBrochure.getHouseEndTimeFw())
                .set(EduSchool::getHouseholdEndTimeFw, eduEnrollmenBrochure.getHouseholdEndTimeFw())
                .set(EduSchool::getAuthOrderByNo, eduEnrollmenBrochure.getAuthOrderByNo())
                .eq(EduSchool::getOrgId, eduSchoolVO.getOrgId());
        eduSchoolService.update(updateWrapper);
        List<EduFormEnroll> eduFormEnrolls = Convert.toList(EduFormEnroll.class, eduFormEnrollBaseList);
        //查询是否是第一次保存
        LambdaQueryWrapper<EduFormEnroll> queryWrapper = new QueryWrapper<EduFormEnroll>().lambda()
                .eq(EduFormEnroll::getFormRefId, eduEnrollmenBrochure.getEnrollmenBrochureId());
        List<EduFormEnroll> list = eduFormEnrollService.list(queryWrapper);
        //新增
        for (EduFormEnroll eduFormEnroll : eduFormEnrolls) {
            //查询局端是否有动态表单
            QueryWrapper<EduFormEnrollParamsBase> paramsBaseQueryWrapper = new QueryWrapper<>();
            paramsBaseQueryWrapper.lambda().eq(EduFormEnrollParamsBase::getFormId, eduFormEnroll.getFormId());
            List<EduFormEnrollParamsBase> eduFormEnrollParamsBaseList = eduFormEnrollParamsBaseService.list(paramsBaseQueryWrapper);
            if (CollectionUtils.isEmpty(list)) {
                eduFormEnroll.setFormId(IdUtil.simpleUUID());
                eduFormEnroll.setSchoolId(eduEnrollmenBrochure.getSchoolId());
                eduFormEnroll.setFormRefId(eduEnrollmenBrochure.getEnrollmenBrochureId());
                //表单参数
                if (!CollectionUtils.isEmpty(eduFormEnrollParamsBaseList)) {
                    List<EduFormEnrollParams> eduFormEnrollParams = Convert.toList(EduFormEnrollParams.class, eduFormEnrollParamsBaseList);
                    eduFormEnrollParams.forEach(item -> {
                        item.setFormConfigId(IdUtil.simpleUUID());
                        item.setFormId(eduFormEnroll.getFormId());
                    });
                    eduFormEnrollParamsService.saveBatch(eduFormEnrollParams);
                }
                eduFormEnrollService.save(eduFormEnroll);
            }
        }
        clearCacheForm(eduFormEnrolls.get(0).getFormId());
        clearCache(eduEnrollmenBrochure.getEnrollmenBrochureId());
        clearCacheAreaCode(eduEnrollmenBrochure.getAreaCode());
        eduSchoolService.clearCache(eduEnrollmenBrochure.getSchoolId(),eduEnrollmenBrochure.getAreaCode());
        return eduEnrollmenBrochure.getEnrollmenBrochureId();
    }

    @Override
    @OpApi(funcCode = "EduenrollmenJh0013", title = "义务教育关联特殊通道表单", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "EduenrollmenJh0013",
            operationName = "义务教育关联特殊通道表单",
            dataType = "articleId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void saveYwSpecialFormInfo(EduFormSpecialVO vo) {
        String brochureId = vo.getEnrollmenBrochureId();
        Assert.hasText(brochureId, "招生方案id不能为空");
        EduEnrollmenBrochure brochure = eduEnrollmenBrochureService.getById(brochureId);
        String formSpecialId = vo.getFormSpecialId();
        if (!ObjectUtils.isEmpty(brochure.getSpecialIds())) {
            String[] specialIds = StringUtils.split(brochure.getSpecialIds(), ",");
            List<String> specialIdtList = new ArrayList<>(specialIds.length);
            Collections.addAll(specialIdtList, specialIds);
            if (vo.getChecked()) {
                specialIdtList.add(formSpecialId);
                brochure.setSpecialIds(String.join(",", specialIdtList));
            } else {
                brochure.setSpecialIds(
                        specialIdtList.stream()
                                .filter(item -> !StringUtils.equals(item, formSpecialId))
                                .collect(Collectors.joining(",")));
            }
        } else {
            if (vo.getChecked()) {
                brochure.setSpecialIds(formSpecialId);
            }
        }
        eduEnrollmenBrochureService.updateById(brochure);
    }

    @Override
    @OpApi(funcCode = "EduenrollmenJh0014", title = "查询报名类别", funcType = FuncTypeEnum.query,publicFlag = BoolEnum.TRUE)
    public List<EduFormEnrollVO> getEnrollConfigName(EduFormEnrollSchoolVO vo) {
        if (StringUtils.isBlank(vo.getSchoolId())) {
            throw new AppException("学校Id为空");
        }
        if (vo.getYear() == null) {
            vo.setYear(eduHelper.thisYear());
        }
        LambdaQueryWrapper<EduFormEnroll> eq =
                new QueryWrapper<EduFormEnroll>().lambda()
                        .select(EduFormEnroll::getFormName, EduFormEnroll::getConfigName,EduFormEnroll::getFormId,EduFormEnroll::getOrderNo)
                        .eq(EduFormEnroll::getSchoolId, vo.getSchoolId()).eq(EduFormEnroll::getYear, vo.getYear())
                        .orderByAsc(EduFormEnroll::getOrderNo);
        List<EduFormEnroll> formEnrolls = eduFormEnrollService.list(eq);
        List<EduFormEnrollVO> formEnrollVOList = Convert.toList(EduFormEnrollVO.class, formEnrolls);
        return formEnrollVOList;
    }

    @Override
    @OpApi(funcCode = "EduenrollmenJh0015", title = "根据学校id和当前报名类别获取可选降级类别", funcType = FuncTypeEnum.query,publicFlag = BoolEnum.TRUE)
    public List<SelectableConfigName> getSelectableConfigName(EduFormEnrollSchoolVO vo) {
        if (StringUtils.isBlank(vo.getSchoolId())){
            throw new AppException("学校id不能为空");
        }
        if (StringUtils.isBlank(vo.getConfigNameAlias())){
            throw new AppException("报名类别别名不能为空");
        }
        Integer year = eduHelper.thisYear();
        //显示可选类别
        ArrayList<SelectableConfigName> configNameArrayList = new ArrayList<>();
        EduSchoolVO schoolVO = eduSchoolService.getDetail(vo.getSchoolId());
        if (ObjectUtils.isEmpty(schoolVO)){
            throw new AppException("学校信息不存在");
        }
        //限定公办学校才能降级
        if (!SchoolNatureEnum.gb.getType().equals(schoolVO.getSchoolNature())){
            return new ArrayList<>();
        }

        QueryWrapper<EduFormEnroll> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(
                        EduFormEnroll::getFormId,
                        EduFormEnroll::getConfigName,
                        EduFormEnroll::getSchoolId,
                        EduFormEnroll::getOrderNo)
                .eq(EduFormEnroll::getSchoolId, vo.getSchoolId())
                .eq(EduFormEnroll::getYear, year);
        //获取学校的报名表单列表
        List<EduFormEnroll> formEnrollList = eduFormEnrollService.list(queryWrapper);

        if (ConfigNameAliasEnum.zxq.getName().equals(vo.getConfigNameAlias())){
            //房户一致（正学区）  全部都可以降级
            for (EduFormEnroll eduFormEnroll : formEnrollList) {
                List<ConfigNameAliasEnum> enumList = ConfigNameAliasEnum.getByType(eduFormEnroll.getConfigName());
                for (ConfigNameAliasEnum aliasEnum : enumList) {
                    //排除自己
                    if (aliasEnum.getName().equals(vo.getConfigNameAlias())){
                        continue;
                    }
                    //判断学校是否开通服务区
                    if (eduFormEnroll.getConfigName().equals("A") &&
                            BoolEnum.FALSE.getType().equalsIgnoreCase(schoolVO.getEduEnrollmenBrochureVO().getServiceFlag())){
                        //排除 服务区相关枚举
                        if (ConfigNameAliasEnum.fwq.getName().equals(aliasEnum.getName())||
                                ConfigNameAliasEnum.fwqyzm.getName().equals(aliasEnum.getName())){
                            continue;
                        }
                    }
                    SelectableConfigName selectableConfigName = new SelectableConfigName();
                    selectableConfigName.setConfigNameAlias(aliasEnum.getName());
                    selectableConfigName.setConfigName(aliasEnum.getType());
                    selectableConfigName.setOrderNo(eduFormEnroll.getOrderNo());
                    selectableConfigName.setSchoolId(eduFormEnroll.getSchoolId());
                    selectableConfigName.setFormId(eduFormEnroll.getFormId());
                    selectableConfigName.setAborigineFlag("0");
                    selectableConfigName.setServiceFlag("0");
                    selectableConfigName.setSpecialFlag("0");
                    if (aliasEnum.getName().equals(ConfigNameAliasEnum.yzm.getName())) {
                        selectableConfigName.setAborigineFlag("1");
                        selectableConfigName.setServiceFlag("0");
                    }
                    if (aliasEnum.getName().equals(ConfigNameAliasEnum.fwqyzm.getName())) {
                        selectableConfigName.setAborigineFlag("1");
                        selectableConfigName.setServiceFlag("1");
                    }
                    if (aliasEnum.getName().equals(ConfigNameAliasEnum.fwq.getName())) {
                        selectableConfigName.setAborigineFlag("0");
                        selectableConfigName.setServiceFlag("1");
                    }
                    if (aliasEnum.getName().equals(ConfigNameAliasEnum.zxq.getName())) {
                        selectableConfigName.setAborigineFlag("0");
                        selectableConfigName.setServiceFlag("0");
                    }
                    configNameArrayList.add(selectableConfigName);
                }
            }
        } else if (ConfigNameAliasEnum.yzm.getName().equals(vo.getConfigNameAlias()) ||
                ConfigNameAliasEnum.fwqyzm.getName().equals(vo.getConfigNameAlias())||
                ConfigNameAliasEnum.fwq.getName().equals(vo.getConfigNameAlias())
        ){
            //房户一致（原住民）  可降级A类以下的
            for (EduFormEnroll eduFormEnroll : formEnrollList) {
                List<ConfigNameAliasEnum> enumList = ConfigNameAliasEnum.getByType(eduFormEnroll.getConfigName());
                for (ConfigNameAliasEnum aliasEnum : enumList) {
                    //排除自己
                    if (aliasEnum.getName().equals(vo.getConfigNameAlias())){
                        continue;
                    }
                    //判断学校是否开通服务区
                    if (eduFormEnroll.getConfigName().equals("A") &&
                            BoolEnum.FALSE.getType().equalsIgnoreCase(schoolVO.getEduEnrollmenBrochureVO().getServiceFlag())){
                        //排除 服务区相关枚举
                        if (ConfigNameAliasEnum.fwq.getName().equals(aliasEnum.getName())||
                                ConfigNameAliasEnum.fwqyzm.getName().equals(aliasEnum.getName())){
                            continue;
                        }
                    }
                    SelectableConfigName selectableConfigName = new SelectableConfigName();
                    selectableConfigName.setConfigNameAlias(aliasEnum.getName());
                    selectableConfigName.setConfigName(aliasEnum.getType());
                    selectableConfigName.setFormId(eduFormEnroll.getFormId());
                    selectableConfigName.setOrderNo(eduFormEnroll.getOrderNo());
                    selectableConfigName.setSchoolId(eduFormEnroll.getSchoolId());
                    selectableConfigName.setAborigineFlag("0");
                    selectableConfigName.setServiceFlag("0");
                    selectableConfigName.setSpecialFlag("0");
                    if (aliasEnum.getName().equals(ConfigNameAliasEnum.yzm.getName())) {
                        selectableConfigName.setAborigineFlag("1");
                        selectableConfigName.setServiceFlag("0");
                    }
                    if (aliasEnum.getName().equals(ConfigNameAliasEnum.fwqyzm.getName())) {
                        selectableConfigName.setAborigineFlag("1");
                        selectableConfigName.setServiceFlag("1");
                    }
                    if (aliasEnum.getName().equals(ConfigNameAliasEnum.fwq.getName())) {
                        selectableConfigName.setAborigineFlag("0");
                        selectableConfigName.setServiceFlag("1");
                    }
                    if (aliasEnum.getName().equals(ConfigNameAliasEnum.zxq.getName())) {
                        selectableConfigName.setAborigineFlag("0");
                        selectableConfigName.setServiceFlag("0");
                    }
                    configNameArrayList.add(selectableConfigName);
                }
            }
        } else if (ConfigNameAliasEnum.yhwf.getName().equals(vo.getConfigNameAlias())){
            //有户无房  只能降级集体户
            for (EduFormEnroll eduFormEnroll : formEnrollList) {
                if ("特殊户口".equalsIgnoreCase(eduFormEnroll.getConfigName())){
                    List<ConfigNameAliasEnum> enumList = ConfigNameAliasEnum.getByType(eduFormEnroll.getConfigName());
                    for (ConfigNameAliasEnum aliasEnum : enumList) {
                        SelectableConfigName selectableConfigName = new SelectableConfigName();
                        selectableConfigName.setConfigNameAlias(aliasEnum.getName());
                        selectableConfigName.setConfigName(aliasEnum.getType());
                        selectableConfigName.setFormId(eduFormEnroll.getFormId());
                        selectableConfigName.setOrderNo(eduFormEnroll.getOrderNo());
                        selectableConfigName.setSchoolId(eduFormEnroll.getSchoolId());
                        selectableConfigName.setAborigineFlag("0");
                        selectableConfigName.setServiceFlag("0");
                        selectableConfigName.setSpecialFlag("0");
                        configNameArrayList.add(selectableConfigName);
                    }
                }
            }
        }

        //根据配置类别排序
        configNameArrayList.sort(Comparator.comparing(SelectableConfigName::getOrderNo));
        return configNameArrayList;
    }

    @Override
    @OpApi(funcCode = "EduenrollmenJh0016", title = "删除招生表单", funcType = FuncTypeEnum.delete,checkPrivilege = BoolEnum.TRUE)
    @SaveOplog(
            operationType = "EduenrollmenJh0016",
            operationName = "删除招生表单",
            dataType = "formId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void delEnrollParam(EduFormEnrollKeyVO vo) {
        if (StringUtils.isBlank(vo.getFormId())){
            throw new AppException("招生表单Id不能为空");
        }
        Integer year = eduHelper.thisYear();
        LambdaQueryWrapper<EduFormEnroll> queryWrapper = new QueryWrapper<EduFormEnroll>().lambda().eq(EduFormEnroll::getFormId, vo.getFormId()).eq(EduFormEnroll::getYear, year);
        List<EduFormEnroll> eduFormEnrolls = eduFormEnrollService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(eduFormEnrolls)){
            for (EduFormEnroll eduFormEnroll : eduFormEnrolls){
                List<EduFormEnrollParams> list = eduFormEnrollParamsService.list(new QueryWrapper<EduFormEnrollParams>().lambda().eq(EduFormEnrollParams::getFormId, eduFormEnroll.getFormId()));
                if (!CollectionUtils.isEmpty(list)){
                    eduFormEnrollParamsService.remove(new QueryWrapper<EduFormEnrollParams>().lambda().eq(EduFormEnrollParams::getFormId, eduFormEnroll.getFormId()));
                }
                eduFormEnrollService.remove(new QueryWrapper<EduFormEnroll>().lambda().eq(EduFormEnroll::getFormId, eduFormEnroll.getFormId()));
                clearCacheForm(eduFormEnroll.getFormId());
                clearCacheAreaCode(eduFormEnroll.getAreaCode());
            }
        }
    }

    //清除缓存
    private void clearCache(String enrollmenBrochureId) {
        if (StringUtils.isNotBlank(enrollmenBrochureId)) {
            Cache cache = JetCacheUtils.create(EduCache.EDU_BROCHURE_ID, CacheType.BOTH);
            if (cache != null) {
                cache.remove(enrollmenBrochureId);
            }
        }
    }

    private void clearCacheForm(String formId) {
        if (StringUtils.isNotBlank(formId)) {
            Cache cache = JetCacheUtils.create(EduCache.EDU_ENROLL_FORM_PARAMS, CacheType.BOTH);
            if (cache != null) {
                cache.remove(formId);
            }
        }
    }

    private void clearCacheAreaCode(String areaCode) {
        if (StringUtils.isNotBlank(areaCode)) {
            Cache cache = JetCacheUtils.create(EduCache.EDU_BROCHURE_areaCode, CacheType.BOTH);
            if (cache != null) {
                cache.remove(areaCode);
            }
        }
    }

}
